﻿using System;
using System.Collections.Generic;
using System.Text;

namespace HostTool.Protocol
{
    public class Frame
    {
        /*
         * 数据格式 : 帧头 |       数据长度          |
         *                10 02 | 2Byte 高字节在前  | 
         */
        private byte[] data;

        public Frame(byte[] frame, int length)
        {
            if (frame == null) return;
            if (length <= 0) return;
            if (length > frame.Length) return;

            data = new byte[length];
            Array.Copy(frame, data, length);
        }

        /// <summary>
        /// 源端口号
        /// </summary>
        public byte SrcPort
        {
            get
            {
                return data[4];
            }
        }

        /// <summary>
        /// 目的端口号
        /// </summary>
        public byte DestPort
        {
            get
            {
                int SrcAddrLen = data[5];
                return data[6 + SrcAddrLen];
            }
        }

        /// <summary>
        /// 事务
        /// </summary>
        public byte Transaction
        {
            get
            {
                int SrcAddrLen = data[5];
                int DstAddrLen = data[7 + SrcAddrLen];

                return data[8 + SrcAddrLen + DstAddrLen];
            }
        }

        /// <summary>
        /// 命令
        /// </summary>
        public byte Cmd
        {
            get
            {
                int SrcAddrLen = data[5];
                int DstAddrLen = data[7 + SrcAddrLen];

                return data[9 + SrcAddrLen + DstAddrLen];
            }
        }

        /// <summary>
        /// 实际数据开始的偏移量
        /// </summary>
        public int RealDataOffset
        {
            get
            {
                int SrcAddrLen = data[5];
                int DstAddrLen = data[7 + SrcAddrLen];


                return 10 + SrcAddrLen + DstAddrLen;
            }
        }

        /// <summary>
        /// 实际数据的长度
        /// </summary>
        public int RealDataLength
        {
            get { return data.Length - RealDataOffset - 2 - 2; }
        }


        public byte[] RawData
        {
            get { return data; }
        }




    }
}
